package com.leoman.post.dao;

import com.leoman.common.dao.IBaseJpaRepository;
import com.leoman.post.entity.PostComment;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * 帖子评论dao
 * Created by 涂奕恒 on 2016/6/12.
 */
public interface PostCommentDao extends IBaseJpaRepository<PostComment> {

    @Query("select a from PostComment a where a.type = ?1 and a.postBase.id = ?2 and a.status = ?3 order by a.createDate desc")
    List<PostComment> findList(Integer type, Long postId, Integer status);

    @Query("select a from PostComment a where a.type = ?1 and a.postBase.id = ?2 order by a.createDate desc")
    List<PostComment> findList(Integer type, Long postId);

    @Query("select a from PostComment a where a.type = ?1 and a.postBase.id = ?2 order by a.createDate desc")
    Page<PostComment> iPage(Integer type, Long postId, Pageable pageable);

    @Query("select a from PostComment a where a.toPostCommentId = ?1 and a.status = 1")
    List<PostComment> findList(Long postCommentId);

    @SuppressWarnings("unused")
    @Query("select a from PostComment a where a.toUser.id = ?1 and a.status = 1 and a.postBase.deleteStatus <> 1 order by a.createDate desc")
    Page<PostComment> iPageByToUserId(Long toUserId, Pageable pageable);

    @Query("select a from PostComment a where a.fromUser.id = ?1 and a.postBase.deleteStatus <> 1 order by a.createDate desc")
    Page<PostComment> iPageByToUserId2(Long toUserId, Pageable pageable);

    @Query("select a from PostComment a where a.toUser.id = ?1")
    List<PostComment> iFindListByToUserId(Long toUserId);

    @Query("select a from PostComment a where a.toUser.id = ?1 and a.status = 1 and a.postBase.deleteStatus <> 1 order by a.createDate desc")
    Page<PostComment> pageByToUserId(Long toUserId, Pageable pageable);

    @SuppressWarnings("unused")
    @Query("select a from PostComment a where a.fromUser.id = ?1 and a.status = 1")
    List<PostComment> findListByFromUserId(Long fromUserId);

    @Query("select a from PostComment a where a.createDate > ?1 and a.status = ?2")
    List<PostComment> findListNew(Long oldDate, Integer type);

    @Query("select a from PostComment a where a.postBase.id = ?1 and a.status = ?2 and a.fromUser.isCreator = ?3")
    List<PostComment> findListByParams(Long postBaseId, Integer isComment, Integer isCreator);

    // 用户当天评论帖子和回复帖子的次数
    @SuppressWarnings("unused")
    @Query("SELECT COUNT(a) FROM PostComment a WHERE a.fromUser.id = ?1 AND a.createDate >= ?2 AND a.createDate <?3")
    Integer findTodayPostCommentNum(Long fromUserId, Long morningDate, Long nightDate);

    // 用户当天评论/回复帖子的次数
    @SuppressWarnings("unused")
    @Query("SELECT COUNT(a) FROM PostComment a WHERE a.fromUser.id = ?1 AND a.createDate >= ?2 AND a.createDate <?3 AND a.status =?4")
    Integer findTodayPostCommentNum(Long fromUserId, Long morningDate, Long nightDate, Integer status);

    @Query("select a from PostComment a where a.toPostCommentId = ?1")
    List<PostComment> findListByCommentId(Long commentId);

    @Query("select a from PostComment a where a.postBase.id = ?1")
    List<PostComment> findByPostId(Long postId);
}
